﻿using DotNet.Utilities;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.ViewModels;
using JinfuyuanProject.Service.Interface;
using JinfuyuanProject.Service.Service;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JinfuyuanProject.Controllers
{
    public class HomeController : Controller
    {
        private readonly JinfuyuanDbContext _db;
        private readonly ILoginService _loginService;

        // GET: Home
        public HomeController(JinfuyuanDbContext jinfuyuanDbContext,ILoginService loginService)
        {
            _loginService = loginService;
        }
        #region 页面
        public ActionResult Index()
        {
            Staff user = Session["user"] as Staff;
            ViewBag.userName = user.Name;
            ViewBag.userNumber = user.Numbers;
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult Main()
        {
            return View();
        }
        #endregion
        #region 接口
        public JsonResult CheckLogin()
        {
            
            string account = Request.QueryString["username"];
            string password = Request.QueryString["password"];
           ResMessage res= _loginService.CheckLogin(account, password);
            return Json(res,JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetMenu()
        {
            Staff user = Session["user"] as Staff;
            int rId = user?.RoleId ?? 0; // 如果user为空，rId默认为0
            if (rId == 0)
            {
                return Json(new { code = 1, msg = "没有权限" }, JsonRequestBehavior.AllowGet);
            }

            // 调用服务层的 GetMenuForRole 方法
            var result =_loginService.GetMenuForRole(rId);

            if (result == null)
            {
                return Json(new { code = 1, msg = "获取菜单失败" }, JsonRequestBehavior.AllowGet);
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    
        #endregion
    }
}